#!  /usr/bin/env python
# ecoding=utf-8

import redis
import json
from datetime import date, datetime
from ..setting import *

rds = redis.StrictRedis(host=REDIS_IP, port=REDIS_PORT,password=REDIS_PASSWORD,decode_responses=True)
template_urls_set = 'myspider:template_urls'

class CJsonEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)
class TemplateUrlCacheDao():

    @staticmethod
    def hset(attr,val):
        rds.hset(template_urls_set,attr,val)

    @staticmethod
    def hlen():
        # 长度
        return rds.hlen(template_urls_set)

    # 获取值
    @staticmethod
    def hget(attr):
        st=rds.hget(template_urls_set, attr)
        st=str(st).replace("datetime.datetime","datetime")
        return eval(st)

    # key是否存在
    @staticmethod
    def hexists(attr):
        return rds.hexists(template_urls_set, attr)

    # 删除属性(可以批量删除)
    @staticmethod
    def hdel(attr):
        return rds.hdel(template_urls_set, attr)

    @staticmethod
    def hkeys():
        return rds.hkeys(template_urls_set)


